.\" Copyright (c) 1999 Tim Singletary
.\" No copyright is claimed.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $FreeBSD: src/lib/libc/db/man/dbm.3,v 1.10 2009/01/30 15:28:35 gabor Exp $
.\"
.Dd April 16, 2006
.Dt DBM 3
.Os
.Sh NAME
.Nm dbm_clearerr ,
.Nm dbm_close ,
.Nm dbm_delete ,
.Nm dbm_dirfno ,
.Nm dbm_error ,
.Nm dbm_fetch ,
.Nm dbm_firstkey ,
.Nm dbm_nextkey ,
.Nm dbm_open ,
.Nm dbm_store
.Nd database access functions
.Sh SYNOPSIS
.In ndbm.h
.Ft int
.Fo dbm_clearerr
.Fa "DBM *db"
.Fc
.Ft void
.Fo dbm_close
.Fa "DBM *db"
.Fc
.Ft int
.Fo dbm_delete
.Fa "DBM *db"
.Fa "datum key"
.Fc
.Ft int
.Fo dbm_dirfno
.Fa "DBM *db"
.Fc
.Ft int
.Fo dbm_error
.Fa "DBM *db"
.Fc
.Ft datum
.Fo dbm_fetch
.Fa "DBM *db"
.Fa "datum key"
.Fc
.Ft datum
.Fo dbm_firstkey
.Fa "DBM *db"
.Fc
.Ft datum
.Fo dbm_nextkey
.Fa "DBM *db"
.Fc
.Ft DBM *
.Fo dbm_open
.Fa "const char *file"
.Fa "int open_flags"
.Fa "mode_t file_mode"
.Fc
.Ft int
.Fo dbm_store
.Fa "DBM *db"
.Fa "datum key"
.Fa "datum content"
.Fa "int store_mode"
.Fc
.Sh DESCRIPTION
Database access functions.
These functions are implemented using
.Xr dbopen 3
with a
.Xr hash 3
database.
.Pp
.Vt datum
is declared in
.In ndbm.h :
.Bd -literal
typedef struct {
	char *dptr;
	int dsize;
} datum;
.Ed
.Pp
The
.Fn dbm_open file open_flags file_mode
function
opens or creates a database file.
The
.Fa file
argument
is the basename of the file containing
the database; the actual database has a
.Pa .db
suffix.
I.e., if
.Fa file
is
.Qq Li /home/me/mystuff
then the actual database is in the file
.Pa /home/me/mystuff.db .
The
.Fa open_flags
and
.Fa file_mode
arguments
are passed to
.Xr open 2 .
.Pq Dv O_RDWR | O_CREAT
is a typical value for
.Fa open_flags ;
.Li 0660
is a typical value for
.Fa file_mode .
.Dv O_WRONLY
is not allowed in
.Fa open_flags .
The pointer returned by
.Fn dbm_open
identifies the database and is the
.Fa db
argument to the other functions.
The
.Fn dbm_open
function
returns
.Dv NULL
and sets
.Va errno
if there were any errors.
.Pp
The
.Fn dbm_close db
function
closes the database.
.Pp
The
.Fn dbm_store db key content store_mode
function
inserts or replaces an entry in the database.
The
.Fa store_mode
argument
is either
.Dv DBM_INSERT
or
.Dv DBM_REPLACE .
If
.Fa store_mode
is
.Dv DBM_INSERT
and the database already contains an entry for
.Fa key ,
that entry is not replaced.
Otherwise the entry is replaced or inserted.
The
.Fn dbm_store
function
normally returns zero but returns 1 if the entry could not be
inserted (because
.Fa store_mode
is
.Dv DBM_INSERT ,
and an entry with
.Fa key
already exists) or returns -1 and sets
.Va errno
if there were any errors.
.Pp
The
.Fn dbm_fetch db key
function
returns
.Dv NULL
or the
.Fa content
corresponding to
.Fa key .
.Pp
The
.Fn dbm_delete db key
function
deletes the entry for
.Fa key .
The
.Fn dbm_delete
function
normally returns zero but returns 1 if there was no entry with
.Fa key
in the database or returns -1 and sets
.Va errno
if there were any errors.
.Pp
The
.Fn dbm_firstkey db
function
returns the first key in the database.
The
.Fn dbm_nextkey db
function
returns subsequent keys.
The
.Fn db_firstkey
function
must be called before
.Fn dbm_nextkey .
The order in which keys are returned is unspecified and may appear
random.
The
.Fn dbm_nextkey
function
returns
.Dv NULL
after all keys have been returned.
.Pp
The
.Fn dbm_error db
function
returns the
.Va errno
value of the most recent error.
The
.Fn dbm_clearerr db
function
resets this value to 0 and returns 0.
.Pp
The
.Fn dbm_dirfno db
function
returns the file descriptor to the database.
.Sh LEGACY SYNOPSIS
.Fd #include <fcntl.h>
.Fd #include <ndbm.h>
.Pp
The include file
.In ndbm.h
is necessary for all functions.
.Pp
.Ft DBM *
.br
.Fo dbm_open
.Fa "const char *file"
.Fa "int open_flags"
.Fa "int file_mode"
.Fc ;
.Pp
.Fa file_mode
has type
.Vt int .
.Sh SEE ALSO
.Xr open 2 ,
.Xr dbopen 3 ,
.Xr hash 3 ,
.Xr compat 5
.Sh STANDARDS
These functions (except
.Fn dbm_dirfno )
are included in the
.St -susv2 .
